package com.miraksoft.statewatcher.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.Chronometer.OnChronometerTickListener;
import android.widget.TableLayout;

import com.miraksoft.statewatcher.R;
import com.miraksoft.statewatcher.bean.WatchedObjectRuntimeContainer;
import com.miraksoft.statewatcher.bean.WatchedObjectStateRuntime;
import com.miraksoft.statewatcher.helper.DevelopmentHelper;
import com.miraksoft.statewatcher.helper.WatchedObjectUIHelper;
import com.miraksoft.statewatcher.helper.WatcherEngine;

public class ActiveSessionActivity extends Activity {

	// UI components
	TableLayout tableWatchedObjects;
	Chronometer chronometer;
	Button btnStartStop;
	Button btnPauseResume;
	Button btnEnd;
	
	// Service components
	WatcherEngine engine = null;
	
	// Data objects
	WatchedObjectRuntimeContainer container;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_active_session);
		// Show the Up button in the action bar.
		setupActionBar();
		
		tableWatchedObjects = (TableLayout)findViewById(R.id.tableWatchedObjects);
		chronometer = (Chronometer)findViewById(R.id.chronometerActiveSession);
		
		btnStartStop = (Button)findViewById(R.id.buttonActiveSessionStartStop);
		btnPauseResume = (Button)findViewById(R.id.buttonActiveSessionPause);
		btnEnd = (Button)findViewById(R.id.buttonActiveSessionEnd);
		
		initializeView();
		
		engine = new WatcherEngine( container, this, chronometer);
	}
	
	private void initializeView(){
		
		btnEnd.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				engine.end();
				
			}
		});

		btnPauseResume.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				if( engine.isRunning()){
					
					engine.pause();
					btnPauseResume.setText(getString(R.string.text_resume));
					
				} else if( engine.isPaused() ){
					
					engine.resume();
					btnPauseResume.setText(getString(R.string.text_pause));
					
				}
				
			}
		});
		btnPauseResume.setEnabled(false);
		
		btnStartStop.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				if( engine.isStopped() ){
					
					engine.start();
					btnStartStop.setText(getString(R.string.text_stop));
					
					btnPauseResume.setEnabled(true);
					
				} else {
					
					engine.stop();
					btnStartStop.setText(getString(R.string.text_start));
					btnPauseResume.setEnabled(false);
					
				}
				
			}
		});
		
		chronometer.setOnChronometerTickListener( new OnChronometerTickListener() {
			
			@Override
			public void onChronometerTick(Chronometer chronometer) {
				engine.chronometerTickListener();
				
			}
		});
		
		OnClickListener onClickListener = new OnClickListener() {
			
			@Override
			public void onClick(View btnState) {
				WatchedObjectStateRuntime stateRuntime = container.getRuntimeStateByButtonId(btnState.getId());
				System.out.println(btnState.getId());
				engine.onStateClick( stateRuntime );
			}
		};

		container = WatchedObjectUIHelper.inflateWatchedObjectsIntoTable(
					this, tableWatchedObjects,
					(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),
					DevelopmentHelper.getMockedStates(),
					DevelopmentHelper.getMockedObjects(),
					onClickListener);
		
		
	}

	/**
	 * Set up the {@link android.app.ActionBar}.
	 */
	private void setupActionBar() {

		getActionBar().setDisplayHomeAsUpEnabled(true);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.active_session, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
